package com.sgzn.sgpay.service.support;

import com.sgzn.sgpay.model.request.SgsPrivQueryRequest;
import com.sgzn.sgpay.model.request.SgsShopQueryListRequest;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.vo.PrivilegeVo;
import com.sgzn.sgpay.model.response.vo.ShopVo;
import com.sgzn.sgpay.persistence.dao.*;
import com.sgzn.sgpay.persistence.domain.*;
import com.sgzn.sgpay.service.SgsPrivQueryService;
import com.sgzn.sgpay.service.SgsShopService;
import com.sgzn.sgpay.service.converter.PrivReqConverter;
import com.sgzn.sgpay.service.converter.SubMchShopReqConverter;
import com.sgzn.sgpay.util.TranslateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 子商户操作管理器
 * @author zhongzhongyong
 * @version Id: SubMchManager.java, v 0.1 2018/4/17 22:19 zhongzhongyong Exp $$
 */
@Component
@Slf4j
public class SubMchManager {

    @Autowired
    private TMerchantPrivWriteMapper tMerchantPrivWriteMapper;

    @Autowired
    private TMerchantPrivMapper tMerchantPrivMapper;

    @Autowired
    private TSubMchShopWriteMapper tSubMchShopWriteMapper;

    @Autowired
    private TMerchantMapper tMerchantMapper;

    @Autowired
    private TSubMchShopMapper tSubMchShopMapper;

    @Autowired
    private TMchLoginSessionMapper tMchLoginSessionMapper;

    @Resource
    private SgsPrivQueryService sgsPrivQueryService;

    @Autowired
    private SgsShopService sgsShopService;

    /**
     * 赋予子商户菜单访问权限
     * @param privIds
     * @param subMchId
     */
    public void setSubMchPrivs(String[] privIds,String subMchId){
        if(privIds != null && privIds.length > 0){
            List<TMerchantPriv> privList = PrivReqConverter.getBatchInsertMchPrivList(subMchId,privIds);
            Map<String,Object> map = new HashMap<>();
            map.put("list",privList);
            tMerchantPrivWriteMapper.batchInsert(map);
        }
    }

    /**
     * 赋予子商户门店访问权限
     * @param shopIds
     * @param subMchId
     */
    public void setSubMchRelatShop(String[] shopIds,String subMchId){
        if(shopIds != null && shopIds.length > 0){
            List<TSubMchShop> subMchShopList = SubMchShopReqConverter.getBatchInsertMchPrivList(subMchId,shopIds);
            Map<String,Object> map2 = new HashMap<>();
            map2.put("list",subMchShopList);
            tSubMchShopWriteMapper.batchInsert(map2);
        }
    }

    /**
     * 检查主商户和子商户的关联关系
     * @param subMchId
     * @return
     */
    public boolean checkMainSubMchRelat(String mainMchId, String subMchId){
        TMerchantExample example = new TMerchantExample();
        TMerchantExample.Criteria cra = example.createCriteria();
        cra.andMerchantIdEqualTo(subMchId);
        cra.andParentMchIdEqualTo(mainMchId);
        int num = tMerchantMapper.countByExample(example);
        if(num > 0){
            return true;
        }
        return false;
    }

    /**
     * 删除子商户旧菜单访问权限
     * @param subMchId
     * @return
     */
    public void deleteHisPriv(String subMchId){
        if(StringUtils.isNotBlank(subMchId)){
            TMerchantPrivExample example = new TMerchantPrivExample();
            TMerchantPrivExample.Criteria cra = example.createCriteria();
            cra.andMchIdEqualTo(subMchId);
            tMerchantPrivMapper.deleteByExample(example);
        }
    }

    /**
     * 删除子商户旧门店访问权限
     * @param subMchId
     */
    public void deleteHisSubMchShop(String subMchId){
        if(StringUtils.isNotBlank(subMchId)){
            TSubMchShopExample example = new TSubMchShopExample();
            TSubMchShopExample.Criteria cra = example.createCriteria();
            cra.andMchIdEqualTo(subMchId);
            tSubMchShopMapper.deleteByExample(example);
        }
    }

    /**
     * 删除子账号基本信息
     * @param subMchId
     */
    public void deleteSubMch(String subMchId){
        TMerchantExample example = new TMerchantExample();
        TMerchantExample.Criteria cra = example.createCriteria();
        cra.andMerchantIdEqualTo(subMchId);
        tMerchantMapper.deleteByExample(example);

        TMchLoginSessionExample example2 = new TMchLoginSessionExample();
        TMchLoginSessionExample.Criteria cra2 = example2.createCriteria();
        cra2.andMchIdEqualTo(subMchId);
        tMchLoginSessionMapper.deleteByExample(example2);
    }

    /**
     * 获取子商户基础信息
     * @param subMchId
     * @return
     */
    public TMerchant getSubMchBaseInfo(String subMchId){
        TMerchantExample example = new TMerchantExample();
        TMerchantExample.Criteria cra = example.createCriteria();
        cra.andMerchantIdEqualTo(subMchId);
        List<TMerchant> list = tMerchantMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    /**
     * 获取子商户已有功能权限Id
     * @param subMchId
     * @return
     */
    public List<String> getSubMchPrivList(String subMchId){
        List<String> resultList = new ArrayList<>();
        TMerchantPrivExample example = new TMerchantPrivExample();
        TMerchantPrivExample.Criteria cra = example.createCriteria();
        cra.andMchIdEqualTo(subMchId);
        List<TMerchantPriv> list = tMerchantPrivMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(list)){
            for(TMerchantPriv item : list){
                resultList.add(item.getPrivId());
            }
        }
        return resultList;
    }

    /**
     * 获取子商户已有门店权限Id
     * @param subMchId
     * @return
     */
    public List<String> getSubMchShopList(String subMchId){
        List<String> resultList = new ArrayList<>();
        TSubMchShopExample example = new TSubMchShopExample();
        TSubMchShopExample.Criteria cra = example.createCriteria();
        cra.andMchIdEqualTo(subMchId);
        List<TSubMchShop> list = tSubMchShopMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(list)){
            for(TSubMchShop item : list){
                resultList.add(item.getShopId());
            }
        }
        return resultList;
    }


    /**
     * 获取主商户所有权限
     * @param mainMchId
     * @param loginChannel
     * @param reqTime
     * @param initId
     * @return
     */
    public List<PrivilegeVo> getMainMchPrivList(String mainMchId, String loginChannel,
                                                       Date reqTime,String initId){
        List<PrivilegeVo> resultList = new ArrayList<>();
        SgsPrivQueryRequest request = new SgsPrivQueryRequest();
        request.setMchId(mainMchId);
        request.setLoginChannel(loginChannel);
        request.setReqTime(reqTime);
        request.setInitiationID(initId);
        SgsResultResponse<List<TPrivilege>> privResp = sgsPrivQueryService.queryPrivByMchId(request);
        if(TranslateUtil.isSuccessAbs(privResp.getErrorCode())
                && !CollectionUtils.isEmpty(privResp.getResult())){
            List<TPrivilege> privilegeList = privResp.getResult();
            for(TPrivilege item : privilegeList){
                PrivilegeVo vo = new PrivilegeVo();
                vo.setPrivId(item.getPrivId());
                vo.setPrivName(item.getPrivName());
                resultList.add(vo);
            }
        }
        return resultList;
    }

    /**
     * 获取主商户下所有门店
     * @param loginChannel
     * @param reqTime
     * @param initId
     * @return
     */
    public List<ShopVo> getMainMchShopList(String loginChannel,
                                           Date reqTime, String initId, String mchId){
        List<ShopVo> resultList = null;
        SgsShopQueryListRequest request = new SgsShopQueryListRequest();
        request.setLoginChannel(loginChannel);
        request.setReqTime(reqTime);
        request.setInitiationID(initId);
        request.setMchId(mchId);
        SgsResultResponse<List<ShopVo>> shopResp = sgsShopService.queryList(request);
        if(TranslateUtil.isSuccessAbs(shopResp.getErrorCode())
                && !CollectionUtils.isEmpty(shopResp.getResult())){
            resultList = shopResp.getResult();
        }
        return resultList;
    }


}
